$(function(){
	layui.use('form', function () {
		var form = layui.form;
		form.render();
		findByCompetence(form)
		findByPositionId(form)
		$("#addPersonnel").click(function(){
			$.ajax({
				url:"http://localhost:8081/personnel/addPersonnel",
				type:"POST",
				dataType:"json",
				accept:"application/json",
				contentType:"application/json",
				data:JSON.stringify($("#addfrom").serializeObject()),
				success: function (data) {
					if(data.code==200){
						 layer.msg("添加成功", {icon: 6, time: 1000});
					}else{
						 layer.msg("添加失败", {icon: 5, time: 1000});
					}
			    },
			    error: function(data){
				    layer.msg(""+data.message+"", {icon: 5, time: 1000});
			   }
			})
			setTimeout('parent.location.reload();',1000);
		})
	});
})
//添加权限下拉框
function findByCompetence(form){
	$.ajax({
		//请求方式
		type: "GET",
		//请求地址
		url: "http://localhost:8081/competence/findAll",
		dataType: "json",
		success: function (date) {
			if(date.code==0){
				var list = date.data;    //返回的数据
				var government = $("#competenceId");//server为select定义的id
				for (var i = 0; i < list.length; i++) {
					var option = document.createElement("option");  // 创建添加option属性
					option.setAttribute("value", list[i].competenceId); // 给option的value添加值
					option.innerText = list[i].competenceName;   // 打印option对应的纯文本
					government.append(option);           //给select添加option子标签
				}
				form.render("select");            // 刷新select，显示出数据
			}else{
				 layer.msg("请求失败", {icon: 5, time: 1000});
			}
		}
	});
}
//添加部门下拉框
function findByPositionId(form){
	$.ajax({
		//请求方式
		type: "GET",
		//请求地址
		url: "http://localhost:8081/position/findAll",
		dataType: "json",
		success: function (date) {
			if(date.code==0){
				var list = date.data;    //返回的数据
				var government = $("#positionId");//server为select定义的id
				for (var i = 0; i < list.length; i++) {
					var option = document.createElement("option");  // 创建添加option属性
					option.setAttribute("value", list[i].positionId); // 给option的value添加值
					option.innerText = list[i].positionName;   // 打印option对应的纯文本
					government.append(option);           //给select添加option子标签
					form.render("select");            // 刷新select，显示出数据
				}
			}else{
				 layer.msg("请求失败", {icon: 5, time: 1000});
			}
		}
	});
}
$.fn.serializeObject = function() {
	var o = {};
	var a = this.serializeArray();
	$.each(a, function() {
		if (o[this.name]) {
			if (!o[this.name].push) {
				o[this.name] = [ o[this.name] ];
			}
			o[this.name].push(this.value || '');
		} else {
			o[this.name] = this.value || '';
		}
	});
	return o;
};